package oriol.forns.gutierrez;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Random;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.Log;

public class Elements {
	
	ArrayList<Objecte> elements;
	ArrayList<Bitmap> bitmaps;
	ArrayList<InputStream> pantallas;
	ArrayList<InputStream> slots;
	Resources resources;
	BitmapFactory.Options options;
	public boolean acabat;
	
	public Elements(){
		elements = new ArrayList<Objecte>();
		pantallas = new ArrayList<InputStream>();
		slots = new ArrayList<InputStream>();
		bitmaps = new ArrayList<Bitmap>();
		options = new BitmapFactory.Options();
		options.inPreferredConfig = Bitmap.Config.ARGB_8888;
		resources = GV.Instancies.arka.getResources();
		pantallas.add(resources.openRawResource(R.raw.pantalla1));
		pantallas.add(resources.openRawResource(R.raw.pantalla2));
		pantallas.add(resources.openRawResource(R.raw.pantalla3));
		pantallas.add(resources.openRawResource(R.raw.pantalla4));
		pantallas.add(resources.openRawResource(R.raw.pantalla5));
		pantallas.add(resources.openRawResource(R.raw.pantalla6));
		pantallas.add(resources.openRawResource(R.raw.pantalla7));
		pantallas.add(resources.openRawResource(R.raw.pantalla8));
		pantallas.add(resources.openRawResource(R.raw.pantalla9));
		pantallas.add(resources.openRawResource(R.raw.pantalla10));
		slots.add(resources.openRawResource(R.raw.slot1));
		slots.add(resources.openRawResource(R.raw.slot2));
		slots.add(resources.openRawResource(R.raw.slot3));
		loadbitmaps();
		
		try {
			cargar_pant();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public Elements(ArrayList<Integer> elms) {
		elements = new ArrayList<Objecte>();
		pantallas = new ArrayList<InputStream>();
		bitmaps = new ArrayList<Bitmap>();
		options = new BitmapFactory.Options();
		options.inPreferredConfig = Bitmap.Config.ARGB_8888;
		resources = GV.Instancies.prova.getResources();
		loadbitmaps2();
		
		for(int i=0; i<elms.size(); i++) {
			if (elms.get(i) != -1 && elms.get(i) != 8) {
				float tx = (float)(GV.Screen.metrics.widthPixels*0.055);
				float ty = (float)((GV.Screen.metrics.heightPixels/1.5)*0.055);
				float x = tx*(i%18);
				float y = ty*(i/18)+ty;
				elements.add(new Objecte(x, y, 0, 0, tx, ty, bitmaps.get(elms.get(i) ) ) );
			}
		}
	}
	
	public Elements(int pant) {
		elements = new ArrayList<Objecte>();
		pantallas = new ArrayList<InputStream>();
		bitmaps = new ArrayList<Bitmap>();
		options = new BitmapFactory.Options();
		options.inPreferredConfig = Bitmap.Config.ARGB_8888;
		resources = GV.Instancies.arka.getResources();
		
		pantallas.add(resources.openRawResource(R.raw.pantalla1));
		pantallas.add(resources.openRawResource(R.raw.pantalla2));
		pantallas.add(resources.openRawResource(R.raw.pantalla3));
		pantallas.add(resources.openRawResource(R.raw.pantalla4));
		pantallas.add(resources.openRawResource(R.raw.pantalla5));
		pantallas.add(resources.openRawResource(R.raw.pantalla6));
		pantallas.add(resources.openRawResource(R.raw.pantalla7));
		pantallas.add(resources.openRawResource(R.raw.pantalla8));
		pantallas.add(resources.openRawResource(R.raw.pantalla9));
		pantallas.add(resources.openRawResource(R.raw.pantalla10));
		
		loadbitmaps();
		nova_pantalla(pant);
	}
	
	public void nova_pantalla(int x){
		
		acabat = false;
		try {
			if(x < 10)load_stage(x);
			else {
				GV.marcador.vides = 0;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void cargar_pant() throws IOException{
		String str="";
		StringBuffer buf = new StringBuffer();
		InputStream is = slots.get(GV.Instancies.carrega);
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		if (is!=null) {
			while ((str = reader.readLine()) != null) {	
				buf.append(str + "\n" );
			}				
		}		
		is.close();
		
		str = buf.toString();
		String[] sa = str.split(" ");
		for(int i=0; i<304; i++) {
			if (!sa[i].equals("-1") && !sa[i].equals("\n")) {
				float tx = (float)(GV.Screen.metrics.widthPixels*0.055);
				float ty = (float)((GV.Screen.metrics.heightPixels/1.5)*0.055);
				float x = tx*(i%19);
				float y = ty*(i/19)+ty;
				elements.add(new Objecte(x, y, 0, 0, tx, ty, bitmaps.get(Integer.valueOf(sa[i]) ) ) );
			}
		}
		GV.Instancies.pantalla = Integer.valueOf(sa[304]);
		Log.v("long", sa[304]);
	}
	
	public void load_stage(int p) throws IOException {
		String str="";
		StringBuffer buf = new StringBuffer();
		InputStream is = pantallas.get(p);
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		if (is!=null) {							
			while ((str = reader.readLine()) != null) {	
				buf.append(str + "\n" );
			}				
		}		
		is.close();
		
		str = buf.toString();
		String[] sa = str.split(" ");
		for(int i=0; i<sa.length; i++) {
			if (!sa[i].equals("-1") && !sa[i].equals("\n")) {
				float tx = (float)(GV.Screen.metrics.widthPixels*0.055);
				float ty = (float)((GV.Screen.metrics.heightPixels/1.5)*0.055);
				float x = tx*(i%19);
				float y = ty*(i/19)+ty;
				elements.add(new Objecte(x, y, 0, 0, tx, ty, bitmaps.get(Integer.valueOf(sa[i]) ) ) );
			}
		}
	}
	
	private void loadbitmaps2() {
		bitmaps.add(BitmapFactory.decodeResource(resources, R.drawable.blanc, options));
		bitmaps.add(BitmapFactory.decodeResource(resources, R.drawable.blau, options));
		bitmaps.add(BitmapFactory.decodeResource(resources, R.drawable.groc, options));
		bitmaps.add(BitmapFactory.decodeResource(resources, R.drawable.marro, options));
		bitmaps.add(BitmapFactory.decodeResource(resources, R.drawable.negre, options));
		bitmaps.add(BitmapFactory.decodeResource(resources, R.drawable.taronja, options));
		bitmaps.add(BitmapFactory.decodeResource(resources, R.drawable.vermell, options));
		bitmaps.add(BitmapFactory.decodeResource(resources, R.drawable.vert, options));
		bitmaps.add(BitmapFactory.decodeResource(resources, R.drawable.transpa, options));
	}
	
	private void loadbitmaps() {
		bitmaps.add(BitmapFactory.decodeResource(resources, R.drawable.marro, options));
		bitmaps.add(BitmapFactory.decodeResource(resources, R.drawable.blau, options));
		bitmaps.add(BitmapFactory.decodeResource(resources, R.drawable.taronja, options));
		bitmaps.add(BitmapFactory.decodeResource(resources, R.drawable.vermell, options));
		bitmaps.add(BitmapFactory.decodeResource(resources, R.drawable.groc, options));
		bitmaps.add(BitmapFactory.decodeResource(resources, R.drawable.negre, options));
		bitmaps.add(BitmapFactory.decodeResource(resources, R.drawable.blanc, options));
		bitmaps.add(BitmapFactory.decodeResource(resources, R.drawable.vert, options));

	}
	
	public void draw(Canvas canvas){
		for (int i = 0; i < elements.size(); ++i) {
			elements.get(i).draw(canvas);
		}
	}

	public void colisions(Pilota pilota, Powerups power, Shoots shoot) {
		for(int i=elements.size()-1; i>=0; i--) {
			if (pilota.destrueix(elements.get(i).x, elements.get(i).y, elements.get(i).tx, elements.get(i).ty) ||
					shoot.destrueix(elements.get(i).x, elements.get(i).y, elements.get(i).tx, elements.get(i).ty)){
				power.nou_power(elements.get(i).x, elements.get(i).y, elements.get(i).tx);
				elements.remove(i);
			}
		}
		if (elements.isEmpty()) acabat = true;
	}
}
